<?php
/**
 * Created by PhpStorm.
 * User: 何健喔喔喔
 * Date: 2016/12/24
 * Time: 9:21
 */
    function http_curl($url){
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLINFO_HEADER_OUT,true);
        $opt=curl_exec($ch);
        curl_close($ch);
        return $opt;
    }
    function get_token(){
        $appid="wxf2c0b4cfd0018d0e";
        $secret="55926fccdebee063eff40a6604f3b088";
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $json=http_curl($url);
        $json=json_decode($json);
        return $json->access_token;
    }
    $token=get_token();
    var_dump($token);
    $url_gp="https://api.weixin.qq.com/cgi-bin/groups/get?access_token={$token}";
    //var_dump($url_gp);
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url_gp);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLINFO_HEADER_OUT,true);
    $opt=curl_exec($ch);
    curl_close($ch);
    var_dump($opt);
    function set_msg()
    {
    
        $token = get_token();
        //var_dump($token);
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$token}";
        $result = http_curl($url);
        $result = json_decode($result);
        //$result=$result['openid'];
        $result = $result->data->openid;
        var_dump($result);
    
        foreach ($result as $id){
            echo '<pre>';
            echo $id;
            //获取所有的组id，由微信自动分配
            $token = get_token();
            $url_1 = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token={$token}";
            $gp_id = array('openid' => $id);
            $gp_id = json_encode($gp_id);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url_1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $gp_id);
            $data_id = curl_exec($ch);
            //curl_close($ch);
            //var_dump($data_id);
            echo $data_id;
            echo 13;
            /*$data_id = json_decode($data_id);
            $data_id = $data_id['groupid'];*/
            $data_id=json_decode($data_id);
            //$data_id=$data_id[0];
            $data_id=$data_id->groupid;
            var_dump($data_id);
            //echo  json_last_error();
            //实现分组发送
            if ($data_id==100){
                echo 'success';
                $template=array(
                    'touser'=>$id,
                    'template_id'=>"Eo_MA64WZd14I2Bzm4D2LEJQyj4r4R5MxgOfIVuOe6A",    //模板的id
                    'url'=>"http://baidu.com",
                    'topcolor'=>"#FF0000",
                    'data'=>array(
                        'productType'=>array('value'=>"hejian",'color'=>"#00008B"),    //函数传参过来的name
                        'name'=>array('value'=>"hejian",'color'=>'#00008B'),        //函数传参过来的zu
                        'number'=>array('value'=>"hejian",'color'=>'#00008B'),   //时间
                        'expDate'=>array('value'=>"hejian",'color'=>'#00008B'),
                        'remark'=>array('value'=>"hejian",'color'=>'#00008B'),//函数传参过来的ramain
                    )
                );
    
                var_dump($template);
                $formwork=json_encode($template);
    
    
    
    
    
                $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$token}";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $formwork);
                $data = curl_exec($ch);
                curl_close($ch);
                echo "<br>";
            }
            
            //获取用户的基本信息
            $url_info="https://api.weixin.qq.com/cgi-bin/user/info?access_token={$token}&openid={$id}&lang=zh_CN";
            echo '<pre>';
            echo $url_info;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url_info);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $url_info);
            $data_info = curl_exec($ch);
            curl_close($ch);
            var_dump($data_info);
            
            
            
            //echo $data_id;
        }
        /*foreach ($result as $id) {
        
            //获取access_token
            //$access_token = getaccess_token();
            //print_r($id);
            $token = get_token();
            echo $id;
        
        
            //查询用户的分组id；
            $url_1 = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token={$token}";
            $gp_id = array('openid' => $id);
            $gp_id = json_encode($gp_id);
            var_dump($gp_id);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url_1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $gp_id);
            $data_id = curl_exec($ch);
            curl_close($ch);
            var_dump($data_id);
            $data_id = json_decode($data_id);
            $data_id = $data_id['groupid'];
            //var_dump($data_id);
        }*/
        
        
        
    }
    echo set_msg();
           